package com.pdstudio.weather;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

import com.pdstudio.app.Application;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;

import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

public class SettingsActivity extends SwipeBackActivity implements Application.EventHandler{

	private TextView tvVersionNum;
	private View vStatement;
	private View vSupport;
	private View vPush;
	private View vBroadcast;
	private ToggleButton tbBroadcast;
	private ToggleButton tbPush;
	private View customLiveIndexTitleView;
	private TextView txtTitle;

	private View vAuth;
	private ImageView ivBack;

	private int pushHour;
	private int pushMinute;
	private TextView tvPushTime;
	private TextView tvBroadcastTime;
	
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_settings);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));


		customLiveIndexTitleView = findViewById(R.id.title_settings);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("设置");


		tvVersionNum = (TextView) findViewById(R.id.settings_versionnum);
		tvVersionNum.setText(getVersion());

		vStatement = (RelativeLayout) findViewById(R.id.more_page_row5);
		vStatement.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this,
						StatementActivity.class);
				startActivity(intent);
				finish();
			}

		});

		vSupport = (RelativeLayout) findViewById(R.id.more_page_support);
		vSupport.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this,
						SupportActivity.class);
				startActivity(intent);
				finish();
			}
		});

		// 推送设置功能开关
		tbPush = (ToggleButton) findViewById(R.id.togglebutton_push);			
		tbPush.setChecked(mSpUtil.getIsPush());
		tbPush.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {					
					startPushService();
					mSpUtil.setIsPush(true);
					// 使用Toast信息提示
					Toast.makeText(SettingsActivity.this, "打开推送功能",
							Toast.LENGTH_SHORT).show();

				} else {		
					stopPushService();
					mSpUtil.setIsPush(false);						
					// 使用Toast信息提示
					Toast.makeText(SettingsActivity.this, "关闭推送功能",
							Toast.LENGTH_SHORT).show();

				}
			}

		});

		tvPushTime = (TextView) findViewById(R.id.settings_broadcasttime);
		tvPushTime.setText(mSpUtil.getPushTime());
		vPush = (RelativeLayout) findViewById(R.id.more_page_broadcast);
		vPush.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 生成一个TimePickerDialog对象，并显示。显示的TimePickerDialog控件可以选择时分，并设置

				new TimePickerDialog(SettingsActivity.this,
						new OnTimeSetListener() {

							@Override
							public void onTimeSet(TimePicker view,
									int hourOfDay, int minute) {
								// TODO Auto-generated method stub
								pushHour = hourOfDay;
								pushMinute = minute;
								tvPushTime.setText(String.format("%02d",
										pushHour)
										+ ":"
										+ String.format("%02d", pushMinute));
								mSpUtil.setPushTime(tvPushTime.getText().toString());
							}
						}, Calendar.HOUR_OF_DAY, Calendar.MINUTE, true).show();
			}

		});

		// 语音播报设置功能开关
		tbBroadcast = (ToggleButton) findViewById(R.id.togglebutton_broadcast);
		tbBroadcast.setChecked(mSpUtil.getIsBroadcast());
		tbBroadcast.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					mSpUtil.setIsBroadcast(isChecked);	
					// 使用Toast信息提示
					Toast.makeText(SettingsActivity.this, "打开语音播报功能",
							Toast.LENGTH_SHORT).show();

				} else {
					// 获取用户授权preference对象
					mSpUtil.setIsBroadcast(isChecked);
					// 使用Toast信息提示
					Toast.makeText(SettingsActivity.this, "关闭语音播报功能",
							Toast.LENGTH_SHORT).show();
				}
			}

		});
		
		tvBroadcastTime = (TextView) findViewById(R.id.settings_broadcasttime2);
		tvBroadcastTime.setText(mSpUtil.getBroadcastTime());
		vBroadcast = (RelativeLayout) findViewById(R.id.more_page_broadcasttime);
		vBroadcast.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 生成一个TimePickerDialog对象，并显示。显示的TimePickerDialog控件可以选择时分，并设置
				new TimePickerDialog(SettingsActivity.this,
						new OnTimeSetListener() {
							@Override
							public void onTimeSet(TimePicker view,
									int hourOfDay, int minute) {
								// TODO Auto-generated method stub
								pushHour = hourOfDay;
								pushMinute = minute;
								tvBroadcastTime.setText(String.format("%02d",
										pushHour)
										+ ":"
										+ String.format("%02d", pushMinute));
								mSpUtil.setBroadcastTime(tvBroadcastTime.getText().toString());
								
								//使用极光推送来设置推送的时间
								//此代码表示周一到周五、上午10点到晚上23点，都可以推送。
								//JPushInterface.setPushTime(getApplicationContext(), days, 10, 23);
								Set<Integer> days = new HashSet<Integer>();
								days.add(1);
								days.add(2);
								days.add(3);
								days.add(4);
								days.add(5);
								days.add(6);
								days.add(7);
								JPushInterface.setPushTime(getApplicationContext(), days, 19, 20);


							}
						}, Calendar.HOUR_OF_DAY, Calendar.MINUTE, true).show();
			}

		});
		
		
		vAuth = (LinearLayout) findViewById(R.id.setting_auth);
		vAuth.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this,
						UserAuthActivity.class);
				startActivity(intent);
				finish();
			}
		});

		ivBack = (ImageView) findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SettingsActivity.this.finish();

			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public String getVersion() {
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			String version = info.versionName;
			return this.getString(R.string.version_name) + version;
		} catch (Exception e) {
			e.printStackTrace();
			return this.getString(R.string.can_not_find_version_name);
		}
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * 停止极光推送服务
	 * */
	private void stopPushService()
	{
		if(!JPushInterface.isPushStopped(getApplicationContext())){
			JPushInterface.stopPush(getApplicationContext());
		}
	}
	
	/*
	 * 启动极光推送服务
	 * */
	private void startPushService()
	{
		if(JPushInterface.isPushStopped(getApplicationContext())){
			JPushInterface.resumePush(getApplicationContext());
		}
	}
}
